/**
 * RenweiWu
 * PlatformAnalyzesController.java
 * 2017年8月30日
 * PlatformAnalyzesController
 * TODO
 * 
 */
package com.etrans.web.gps.business.report.controller;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.report.domain.PlatformAnalyseCarType;
import com.etrans.web.gps.business.report.domain.PlatformAnalyzes;
import com.etrans.web.gps.business.report.service.PlatformAnalyzesService;
import com.etrans.web.gps.business.system.service.CommonService;
import com.etrans.web.gps.util.DateUtil;

/**
 * @Description:TODO
 * @author RenweiWu
 * @date 2017年8月30日
 * @ClassName:  PlatformAnalyzesController  
 * @param:   
 */
@Controller
@RequestMapping("/base/report/PlatformAnalyzes/")
public class PlatformAnalyzesController extends BaseController{
	
	@Autowired
	private PlatformAnalyzesService platformAnalyzesService;
	@Autowired
	private CommonService commonService;
	
	@RequestMapping("toPlatformAnalyzes.html")
	public String toPlatformAnalyzes(){		
		return "views/business/report/platformanalyzes";
	}
	
	@RequestMapping("toqueryPlatformAnalyzes.html")
	public String toqueryPlatformAnalyzes(String  endTime,String startTime,String platfromid,Model model){
		model.addAttribute("endTime",endTime);
		model.addAttribute("startTime",startTime);
		model.addAttribute("platfromid",platfromid);
		if(platfromid!=null){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("platformid",platfromid);
			String platfromName = commonService.queryplatfromName(map);
			model.addAttribute("platfromName",platfromName);
		}
		return "views/business/report/platformanalyzes";
	}
	
	@RequestMapping("toPlatformAnalyzesAL.html")
	public String toPlatformAnalyzesAL(String platfromid,Model model){
		model.addAttribute("platfromid",platfromid);
		return "views/business/report/platformanalyzesAL";
	}
	@RequestMapping("toPlatformAnalyzesQC.html")
	public String toPlatformAnalyzesQC(Model model,String platfromid){
		model.addAttribute("platfromid", platfromid);
		return "views/business/report/platformanalyzesQC";
	}
	
	@RequestMapping("queryPlatformAnalyzes.html")
	public @ResponseBody Map<String,Object>  queryplatform(String veidoo,String analyse_date,String gnss_centerid){
		int areaId= this.getSysUser().getArea_id();	
		Map<String,Object> map =null;
		try {
			map = platformAnalyzesService.queryAnalyzes(veidoo,analyse_date,areaId,gnss_centerid);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return map;
	}
	
	@RequestMapping("queryPlatform.html")
	public @ResponseBody List<HashMap<String,Object>> getQueryEnterAndPlatByRole(HttpServletRequest request){
		String name = request.getParameter("name");
		int areaId= this.getSysUser().getArea_id();
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("FullID", String.format("%08d",areaId));
		map.put("name", name);
		map.put("selected", "selected");
		List<HashMap<String,Object>> list = commonService.selectqueryplatformbyAreaID(map);
		return list;
	}
	
	@RequestMapping("queryweek.html")
	public @ResponseBody List<HashMap<String,Object>>  queryweek(){
		SimpleDateFormat SD = new SimpleDateFormat("yyyy-dd");
		String str = SD.format(new Date());
		int year = Integer.parseInt(str.split("-")[0]);
        Calendar c=Calendar.getInstance();
        int week = c.get(Calendar.WEEK_OF_YEAR);
        Map<String,Object> map ;
        List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
        for (int i = 0; i < 12; i++) {
        	week--;
            HashMap<String,Object> maps = new HashMap<String,Object>();
        	map = DateUtil.getYearAndWeekByDate(year,week);
        	maps.put("value",year+"-"+week);
        	maps.put("text",year+"年第"+week+"周 ("+((String)map.get("Monday")).substring(5)+"日至"+((String)map.get("Sunday")).substring(5)+"日)");
        	list.add(maps);
		}
		return list;
	}
	
	@RequestMapping("showShadowRegion.html")
	public @ResponseBody List<PlatformAnalyzes>  showShadowRegion(String gnss_centerid,String analyse_date,String veidoo){
		int areaId= this.getSysUser().getArea_id();
		List<PlatformAnalyzes> list = platformAnalyzesService.getShadowRegion(areaId,gnss_centerid,analyse_date,veidoo);
		return list;
	}
	
	@RequestMapping("queryCar.html")
	public @ResponseBody List<PlatformAnalyseCarType>  queryCar(String platformname,String startTime,String endTime,String platformid){
		List<PlatformAnalyseCarType> list = null;
		return list;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("export.html")
	public void export(HttpServletRequest request, HttpServletResponse response,@RequestBody String paramsGrid,
			String colModel, String title, String imgsrc, String sheetNames){
		DecimalFormat df = new DecimalFormat("0.00");
		Map<String, Object> paramMap=getReqParameters();
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		List<String> imgsrcList   = JSON.parseObject(imgsrc, List.class);
		List<String> sheetList   = JSON.parseObject(sheetNames, List.class);
		int areaId= this.getSysUser().getArea_id();
		Map<String, Object> map = null;
		try {
			map = platformAnalyzesService.queryAnalyzes((String)paramMap.get("veidoo"),(String)paramMap.get("analyse_date"),areaId,(String)paramMap.get("gnss_centerid"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		List<PlatformAnalyzes> list = (List<PlatformAnalyzes>)map.get("listAnalyzes");
		List<PlatformAnalyseCarType> carlist = (List<PlatformAnalyseCarType>)map.get("list");
		
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		List rowsList = new ArrayList<>();
		for (PlatformAnalyzes analyzes : list) {
			HashMap<String, Object> map1 = new HashMap<String, Object>();
			map1.put("platform_name", analyzes.getPlatform_name());
			map1.put("owner_unit",analyzes.getOwner_unit() );
			map1.put("disconnect_count", analyzes.getDisconnect_count());
			map1.put("online_time",analyzes.getOnline_time()/60);
			map1.put("online_rate",df.format(analyzes.getOnline_rate()));
			rows.add(map1);
		}
		rowsList.add(rows);
		List<HashMap<String, Object>> rows1 = new ArrayList<HashMap<String, Object>>();
		for (PlatformAnalyzes analyzes1 : list) {
			HashMap<String, Object> map2 = new HashMap<String, Object>();
			map2.put("platform_name", analyzes1.getPlatform_name());
			map2.put("vehicle_count",analyzes1.getVehicle_count() );
			map2.put("vehicle_access_count", analyzes1.getVehicle_access_count());
			map2.put("vehicle_access_rate",analyzes1.getVehicle_count()>0?df.format(((double)analyzes1.getVehicle_access_count()/analyzes1.getVehicle_count())*100):"0");
			map2.put("vehicle_online_count",analyzes1.getVehicle_online_count() );
			map2.put("vehicle_online_rate", analyzes1.getVehicle_access_count()>0?df.format(((double)analyzes1.getVehicle_online_count()/analyzes1.getVehicle_access_count())*100):"0");
			rows1.add(map2);
		}
		rowsList.add(rows1);
		List<HashMap<String, Object>> rows4 = new ArrayList<HashMap<String, Object>>();
		for (PlatformAnalyseCarType CarType : carlist) {
			HashMap<String, Object> map3 = new HashMap<String, Object>();
			map3.put("platformName", CarType.getPlatformName());
			map3.put("regularBus",CarType.getRegularBus() );
			map3.put("touringCar", CarType.getTouringCar());
			map3.put("dangerCar", CarType.getDangerCar());
			rows4.add(map3);
		}
		rowsList.add(rows4);
		List<HashMap<String, Object>> rows2 = new ArrayList<HashMap<String, Object>>();
		for (PlatformAnalyzes analyzes2 : list) {
			HashMap<String, Object> map4 = new HashMap<String, Object>();
			map4.put("platform_name", analyzes2.getPlatform_name());
			map4.put("alarm_vehicle_count",analyzes2.getAlarm_vehicle_count() );
			map4.put("alert_rate", analyzes2.getVehicle_access_count()>0?df.format(((double)analyzes2.getAlarm_vehicle_count()/analyzes2.getVehicle_access_count())*100):"0");
			map4.put("alarm_count", analyzes2.getAlarm_count());
			map4.put("alarm_processed",analyzes2.getAlarm_processed());
			map4.put("alarm_processed_rate", analyzes2.getAlarm_count()>0?df.format(((double)analyzes2.getAlarm_processed()/analyzes2.getAlarm_count())*100):"0");
			map4.put("overspeed_count", analyzes2.getOverspeed_count());
			map4.put("overspeed_count_avg",analyzes2.getVehicle_online_count()>0?df.format(((double)analyzes2.getOverspeed_count()/analyzes2.getVehicle_online_count())):"0");
			map4.put("tired_duration", analyzes2.getTired_duration()/60);
			map4.put("outTime", analyzes2.getVehicle_online_count()>0?df.format(((double)analyzes2.getTired_duration()/analyzes2.getVehicle_online_count())/60):"0");
			rows2.add(map4);
		}
		rowsList.add(rows2);
		List<HashMap<String, Object>> rows3 = new ArrayList<HashMap<String, Object>>();
		for (PlatformAnalyzes analyzes3 : list) {
			HashMap<String, Object> map5 = new HashMap<String, Object>();
			map5.put("platform_name", analyzes3.getPlatform_name());
			map5.put("exception_vehicle_count",analyzes3.getException_vehicle_count() );
			map5.put("exception_rate",analyzes3.getVehicle_online_count()>0?df.format(((double)analyzes3.getException_vehicle_count()/analyzes3.getVehicle_online_count())*100):"0");
			map5.put("speed_160_count", analyzes3.getSpeed_160_count());
			map5.put("location_exception_count",analyzes3.getLocation_exception_count());
			map5.put("gpstime_exception_count", analyzes3.getGpstime_exception_count());
			map5.put("head_exception_count", analyzes3.getHead_exception_count());
			map5.put("exception_contrast_rate",analyzes3.getTrack_normal()+analyzes3.getException_track_count()>0?df.format(((double)analyzes3.getException_track_count()/(analyzes3.getTrack_normal()+analyzes3.getException_track_count()))*100):"0");
			rows3.add(map5);
		}
		rowsList.add(rows3);
		int index=-1;
		exportExls(request, response,title, removeHeadName(colModelList,index,-1,-1,-1,-1),rowsList,imgsrcList,sheetList);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("QCexport.html")
	public void QCexport(HttpServletRequest request, HttpServletResponse response,@RequestBody String paramsGrid, String colModel,String title,String imgsrc){
		DecimalFormat df = new DecimalFormat("0.00");
		Map<String, Object> paramMap=getReqParameters();
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		int areaId= this.getSysUser().getArea_id();
		Map<String, Object> resultmap = null;
		try {
			resultmap = platformAnalyzesService.queryAnalyzes((String)paramMap.get("veidoo"),(String)paramMap.get("analyse_date"),areaId,(String)paramMap.get("gnss_centerid"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		List<PlatformAnalyzes> list = (List<PlatformAnalyzes>)resultmap.get("listAnalyzes");
		List rowsList = new ArrayList<>();
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		for (PlatformAnalyzes analyzes : list) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ranking", analyzes.getRanking());
			map.put("score",analyzes.getScore() );
			map.put("platform_name", analyzes.getPlatform_name());
			map.put("online_rate", analyzes.getOnline_rate());
			map.put("vehicle_count",analyzes.getVehicle_count());
			map.put("vehicle_access_count", analyzes.getVehicle_access_count());
			map.put("vehicle_access_rate",analyzes.getVehicle_count()>0?df.format(((double)analyzes.getVehicle_access_count()/analyzes.getVehicle_count())*100):0);
			map.put("vehicle_online_rate", analyzes.getVehicle_access_count()>0?df.format((double)analyzes.getVehicle_online_count()/analyzes.getVehicle_access_count()*100):0);
			map.put("data_pass",(analyzes.getTrack_normal()+analyzes.getException_track_count())>0?df.format((1-((double)analyzes.getException_track_count()/(analyzes.getTrack_normal()+analyzes.getException_track_count())))*100):0);
			map.put("track_complete_rate", analyzes.getTrack_complete_rate());
			map.put("travel",analyzes.getVehicle_online_count()>0?df.format((double)analyzes.getLocation_exception_count()/analyzes.getVehicle_online_count()*100):0);
			map.put("adas_alarm_count", analyzes.getAdas_alarm_count());
			rows.add(map);
		}
		rowsList.add(rows);
		int index=0;
		exportExl(request, response,title, removeHeadName(colModelList,index),rowsList,null);
	}
}
